package com.mediacloud.app.newsmodule.fragment.simulcast

import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.eventTrack.CDPTrackReportManager
import com.facebook.drawee.view.SimpleDraweeView
import com.mediacloud.app.assembly.util.Utility
import com.mediacloud.app.assembly.util.loadByFresco
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.model.utils.ViewUtils
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.utils.GridSpacingItemDecoration
import java.text.SimpleDateFormat

class SimulcastAdapter : BaseQuickAdapter<SimulcastBean,BaseViewHolder>(R.layout.item_simulcast_fragment) {

    override fun convert(helper: BaseViewHolder, item: SimulcastBean) {
        val rvZX = helper.getView<RecyclerView>(R.id.rvZX)
        val rvZS = helper.getView<RecyclerView>(R.id.rvZS)
        val rvLatest = helper.getView<RecyclerView>(R.id.rvLatest)
        val imgLogo = helper.getView<SimpleDraweeView>(R.id.imgLogo)
        val layoutNews = helper.getView<View>(R.id.layoutNews)
        val layoutZX = helper.getView<View>(R.id.layoutZX)
        val layoutZS = helper.getView<View>(R.id.layoutZS)
        val itemDate = helper.getView<TextView>(R.id.itemDate)
        val tvZX = helper.getView<TextView>(R.id.tvZX)
        val tvZS = helper.getView<TextView>(R.id.tvZS)

        if (item.newsCastList.isNotEmpty()){
            layoutNews.visibility = View.VISIBLE
            val newsCast = item.newsCastList[0]
            helper.setText(R.id.itemTitle,"${newsCast.title}")
            helper.setText(R.id.itemDate,getDateString(item.dateTime,"yyyy-MM-dd","MM月dd日"))
            imgLogo.loadByFresco(newsCast.logo)
        }else{
            layoutNews.visibility = View.GONE
        }
        if (item.latestFlag == "Y"){//最新一条显示全部
            rvLatest.layoutManager = ViewUtils.generateRecyclerLinearLayoutManager(rvLatest.context,true)
            val simulcastLatestAdapter = SimulcastZSAdapter()
            rvLatest.adapter = simulcastLatestAdapter

            rvLatest.visibility = View.VISIBLE
            layoutZX.visibility = View.GONE
            layoutZS.visibility = View.GONE

            if (item.firstSplitList.isNotEmpty()){
                simulcastLatestAdapter.setNewData(item.firstSplitList)
            }

        }else{
            rvLatest.visibility = View.GONE
            if (item.firstSplitList.isNotEmpty()){
                tvZX.text = "本期资讯"
                layoutZX.visibility = View.VISIBLE
                rvZX.layoutManager = GridLayoutManager(rvZX.context,2)
                val simulcastZXAdapter = SimulcastZXAdapter()
                rvZX.adapter = simulcastZXAdapter
//            if (item.firstSplitList.size > 2){//确保只展示两条数据
//                var list = mutableListOf<SimulcastV2Bean>()
//                for(i in 0 until 2){
//                    list.add(item.firstSplitList[i])
//                }
//                simulcastZXAdapter.setNewData(list)
//            }else{
                simulcastZXAdapter.setNewData(item.firstSplitList)
                //}
            } else{
                layoutZX.visibility = View.GONE
            }
            if (item.secondSplitList.isNotEmpty()){
                tvZS.text = "政声"
                layoutZS.visibility = View.VISIBLE
                rvZS.layoutManager = ViewUtils.generateRecyclerLinearLayoutManager(rvZX.context,true)
                val simulcastZSAdapter = SimulcastZSAdapter()
                rvZS.adapter = simulcastZSAdapter
//            if (item.secondSplitList.size > 3){//确保只展示三条数据
//                var list = mutableListOf<SimulcastV2Bean>()
//                for(i in 0 until 3){
//                    list.add(item.secondSplitList[i])
//                }
//                simulcastZSAdapter.setNewData(list)
//            }else{
                simulcastZSAdapter.setNewData(item.secondSplitList)
                //      }

            }else{
                layoutZS.visibility = View.GONE
            }
        }
        helper.addOnClickListener(R.id.itemDate,R.id.imgLogo,R.id.ivPlay,R.id.tvZXMore,R.id.tvZSMore)
    }

    fun getDateString(dateString:String,inputFormatString:String,outputFormatString: String) : String{
        val inputFormat = SimpleDateFormat(inputFormatString)
        val outputFormat = SimpleDateFormat(outputFormatString)

        try {
            val date = inputFormat.parse(dateString)
            return  outputFormat.format(date)
        } catch (e: Exception) {

        }
        return ""
    }

}